package com.qqb.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qqb.dao.OfficerDao;
import com.qqb.pojo.Officer;
import com.qqb.service.EduService;
import com.qqb.service.JobService;
import com.qqb.service.OfficerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Map;

@Service
public class OfficerServiceImpl implements OfficerService {

    @Resource
    private OfficerDao officerDao;

    @Resource
    private EduService eduService;

    @Resource
    private JobService jobService;

    String now = LocalDate.now().toString();
    /**
     * 分页查询
     *
     * @param condition 查询条件
     * @param pageNum   当前页
     * @param pageSize  每页大小
     * @return
     */
    @Override
    public Page<Officer> searchPageList(Map<String, String> condition, String pageNum, String pageSize) {
        int page_Num = Integer.parseInt(pageNum);
        int page_Size = Integer.parseInt(pageSize);

        Page<Officer> page = PageHelper.startPage(page_Num, page_Size);
        Example example = createExample(condition);
        officerDao.selectByExample(example);

 /*       for (Officer officer : page) {
            String eduNameByCode = eduService.findEduNameByCode(officer.getEdu());
            String jobNameByCode = jobService.findJobNameByCode(officer.getJob());
            officer.setEdu(eduNameByCode);
            officer.setJob(jobNameByCode);
        }*/
        return page;
    }



    @Transactional
    @Override
    public void add(Officer officer) {
        String maxOfficerIdStr = officerDao.getMaxOfficerId();
        int maxOfficerId = Integer.parseInt(maxOfficerIdStr.replace("M", "")) + 1;
        StringBuilder maxId = new StringBuilder("M" + maxOfficerId);
        int maxIdLength = maxId.length();
        for (int i = 0; i < 10 - maxIdLength+1; i++) {
            maxId.insert(1, "0");
        }

        officer.setUpdateDate(now); //更新日期
        officer.setRegDate(now); //登记日期
        officer.setOid(maxId.toString());

        officerDao.insert(officer);
    }

    @Override
    public void update(Officer officer) {
        //officer.setEdu(officerDao.getEduNum(officer.getEdu()));
        //officer.setJob(officerDao.getJobNum(officer.getJob()));
        officer.setUpdateDate(now);
        officerDao.updateByPrimaryKeySelective(officer);
    }

    @Override
    public void delete(String[] ids) {
        Example example = new Example(Officer.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("oid", Arrays.asList(ids));
        officerDao.deleteByExample(example);

    }

    /**
     * 条件构造
     *
     * @param condition
     * @return
     */
    public Example createExample(Map<String, String> condition) {
        Example example = new Example(Officer.class);
        Example.Criteria criteria = example.createCriteria();

        if (condition != null && !condition.isEmpty()) {
            /*
            * 如果主键不为空也就是存在 那么就在sql语句where条件里  拼上主键这条约束
            * */
            if (condition.get("oid") != null && !"".equals(condition.get("oid"))) {
                criteria.andEqualTo("oid", condition.get("oid"));
            }
            if (condition.get("offName") != null && !"".equals(condition.get("offName"))) {
                criteria.andLike("offName", "%" + condition.get("offName") + "%");
            }
            if (condition.get("job") != null && !"".equals(condition.get("job"))) {
                criteria.andLike("job", "%" + condition.get("job") + "%");
            }
            if (condition.get("cid") != null && !"".equals(condition.get("cid"))) {
                criteria.andEqualTo("cid", condition.get("cid"));
            }
            if (condition.get("sex") != null && !"".equals(condition.get("sex"))) {
                criteria.andEqualTo("sex", condition.get("sex"));
            }
            if (condition.get("birthday") != null && !"".equals(condition.get("birthday"))) {
                criteria.andEqualTo("birthday", condition.get("birthday"));
            }
            if (condition.get("edu") != null && !"".equals(condition.get("edu"))) {
                criteria.andEqualTo("edu", condition.get("edu"));
            }
            if (condition.get("regDate") != null && !"".equals(condition.get("regDate"))) {
                criteria.andEqualTo("regDate", condition.get("regDate"));
            }
            if (condition.get("updateDate") != null && !"".equals(condition.get("updateDate"))) {
                criteria.andEqualTo("updateDate", condition.get("updateDate"));
            }
        }
        return example;
    }
}
